package com.cnxdc.demo.service.impl;

import com.cnxdc.demo.mapper.ArticleMapper;
import com.cnxdc.demo.service.ArticleService;
import com.cnxdc.demo.utils.ThreadLocalUtil;
import com.cnxdc.demo.vo.Article;
import com.cnxdc.demo.vo.PageBean;
//import com.github.pagehelper.Page;
//import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public int add(Article article) {
        // 首先 拿到用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid=(Integer) map.get("id");
        // 设置用户id 创建时间  修改时间
        article.setCreateUser(userid);
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        //保存
        int count = articleMapper.add(article);
        return count;
    }

    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
//        Map<String,Object> map = ThreadLocalUtil.get();
//        Integer userid=(Integer) map.get("id");
//        PageBean<Article> pageBean = new PageBean<>();//一会儿就要向这个中传递数据，并返回给控制器
//        PageHelper.startPage(pageNum,pageSize);//开启分页
//        List<Article> list = articleMapper.list(categoryId, userid, state);
//        Page<Article> page = (Page<Article>) list;//分页插件会拦截我们的sql执行，并且拦截返回的数据进行包装
//        pageBean.setTotal(page.getTotal());
//        pageBean.setItems(page.getResult());
//        return pageBean;
        return null;
    }
}
